package com.company.笔试.栈和排序;

import java.util.*;


public class Solution {
    /**
     * 栈排序
     * @param a int整型一维数组 描述入栈顺序
     * @return int整型一维数组
     */
    public int[] solve (int[] a) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < a.length; i++) {
            if (stack.isEmpty()){
                stack.add(a[i]);
            }else {
                Integer peek = stack.peek();

                if (a[i]<peek){
                    if (i+1==a.length){
                        stack.pop();
                    }else {
                        stack.add(a[i]);
                    }

                }else if (a[i]>peek){
                    stack.add(a[i]);
                    if (i+1<=a.length && a[i]>a[i+1]){
                        stack.pop();
                    }
                }
            }
        }
        return null;
    }

}